/**
 * 经典书籍插件
 * 提供中国古典文学和现代文学经典作品
 */

import { BookSource } from './defined/booksource';
import { SearchOptions } from './defined/search-options';
import { DetailPageData } from './defined/detail-page-data';
import { ChapterParseObject } from './defined/chapter-parse-object';
import { allClassicBooks } from './data/classic-books-data';

export class ClassicBooksPlugin implements BookSource {
  public readonly GROUP = '经典文库';
  public readonly NAME = '经典书籍';
  public readonly VERSION = '1.0.0';
  public readonly AUTHOR = 'ReadCat';
  public readonly DESCRIPTION = '提供中国古典文学和现代文学经典作品';
  public readonly BASE_URL = 'classic://books';

  /**
   * 搜索书籍
   */
  async search(searchKey: string, options?: SearchOptions): Promise<DetailPageData[]> {
    const results: DetailPageData[] = [];
    const keyword = searchKey.toLowerCase().trim();
    
    if (!keyword) {
      // 如果没有搜索关键词，返回所有经典书籍
      return allClassicBooks.map(book => ({
        bookname: book.bookname,
        author: book.author,
        intro: book.intro,
        coverImageUrl: book.coverImageUrl,
        detailPageUrl: book.detailPageUrl,
        latestChapterTitle: book.latestChapterTitle,
        chapterList: book.chapterList,
      }));
    }
    
    // 根据书名和作者搜索
    for (const book of allClassicBooks) {
      const bookname = book.bookname.toLowerCase();
      const author = book.author.toLowerCase();
      
      if (bookname.includes(keyword) || author.includes(keyword) || 
          keyword.includes(bookname) || keyword.includes(author)) {
        results.push({
          bookname: book.bookname,
          author: book.author,
          intro: book.intro,
          coverImageUrl: book.coverImageUrl,
          detailPageUrl: book.detailPageUrl,
          latestChapterTitle: book.latestChapterTitle,
          chapterList: book.chapterList,
        });
      }
    }
    
    return results;
  }

  /**
   * 获取书籍详情
   */
  async getDetail(detailPageUrl: string): Promise<DetailPageData> {
    const bookId = detailPageUrl.replace('classic://book/', '');
    const book = allClassicBooks.find(b => b.id === bookId);
    
    if (!book) {
      throw new Error(`未找到书籍: ${detailPageUrl}`);
    }
    
    return {
      bookname: book.bookname,
      author: book.author,
      intro: book.intro,
      coverImageUrl: book.coverImageUrl,
      detailPageUrl: book.detailPageUrl,
      latestChapterTitle: book.latestChapterTitle,
      chapterList: book.chapterList,
    };
  }

  /**
   * 获取章节内容
   */
  async getContent(chapterUrl: string): Promise<ChapterParseObject> {
    // 解析章节URL
    const urlParts = chapterUrl.replace('classic://chapter/', '').split('/');
    const bookId = urlParts[0];
    const chapterNum = urlParts[1];
    
    const book = allClassicBooks.find(b => b.id === bookId);
    if (!book) {
      throw new Error(`未找到书籍: ${bookId}`);
    }
    
    // 返回示例内容
    const content = this.getChapterContent(bookId, chapterNum);
    
    return {
      content: content,
      title: book.chapterList.find(c => c.url === chapterUrl)?.title || `第${chapterNum}章`,
    };
  }

  /**
   * 获取章节内容（示例实现）
   */
  private getChapterContent(bookId: string, chapterNum: string): string {
    return `这里是《${allClassicBooks.find(b => b.id === bookId)?.bookname}》第${chapterNum}章的内容。\n\n由于版权原因，这里只显示示例文本。在实际应用中，您需要从合法的文本源获取完整内容。`;
  }
}